<template>
  <div class="app-container">
    <!-- 军民机业务对话框 -->
    <el-form ref="form" v-loading="loading" :model="form" :rules="rules" label-width="120px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="申请项目名称2" prop="projectName">
            <el-input v-model="form.projectName" placeholder="请输入申请项目名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="适用项目" prop="applyProject">
            <el-radio-group v-model="form.applyProject">
              <el-radio v-for="dict in applyProjectOptions" :label="dict.code" :key="dict.code">
                {{dict.name}}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="适用单位" prop="applyUnit">
            <el-checkbox-group v-model="applyUnit">
              <el-checkbox v-for="applyUnit in applyUnitOptions" :label="applyUnit" :key="applyUnit">{{applyUnit}}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="适用员工" prop="applyStaff">
            <el-checkbox-group v-model="applyStaff">
              <el-checkbox v-for="applyStaff in applyStaffOptions" :label="applyStaff" :key="applyStaff">{{applyStaff}}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-form-item label="视力要求" prop="vision" style="margin-top: 5px">
          <el-input v-model="form.vision" placeholder="请输入视力要求"/>
        </el-form-item>
        <el-col :span="12">
          <el-form-item label="视力复核(年)"  prop="visionReview" >
            <el-input-number v-model="form.visionReview" controls-position="right" :min="0" placeholder="请输入复核年限"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="复核期(年)" prop="reviewPeriod" >
            <el-input-number v-model="form.reviewPeriod" controls-position="right" :min="0" placeholder="请输入复核期,单位:年"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="文件要求" prop="fileSpecification">
              <el-input v-model="form.fileSpecification" type="textarea" placeholder="请输入申报文件要求内容"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="其他要求" prop="otherReasons">
              <el-input v-model="form.otherReasons" type="textarea" placeholder="请输入申报其他要求内容"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="培训大纲" prop="outline">
            <el-radio-group v-model="form.outline">
              <el-radio v-for="dict in outlineStatusOptions" :label="dict.code" :key="dict.code">
                {{dict.name}}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="培训教材" prop="textbook">
            <el-radio-group v-model="form.textbook">
              <el-radio v-for="dict in textbookStatusOptions"  :label="dict.code" :key="dict.code">
                {{dict.name}}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="颁证机构" prop="institution">
            <el-radio-group v-model="form.institution">
              <el-radio v-for="dict in institutionStatusOptions"  :label="dict.code" :key="dict.code">
                {{dict.name}}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="机构名称" prop="institutionName">
            <el-input v-model="form.institutionName"  placeholder="请输入机构名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="民机取证类型" prop="civilAircraftCert">
            <el-radio-group v-model="form.civilAircraftCert">
              <el-radio v-for="dict in civilStatusOptions" :label="dict.code" :key="dict.code">
                {{dict.name}}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="清册标识" prop="bookTag">
            <el-select v-model="form.bookTag"  placeholder="清册标识"  clearable size="small">
              <el-option
                v-for="dict in bookTagOptions"
                :key="dict.code"
                :label="dict.name"
                :value="dict.code"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="其他要求" prop="otherCause">
              <el-input v-model="form.otherCause" type="textarea" placeholder="请输入其他要求内容"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="检验检测员取证" prop="checkoutIsCert">
            <el-radio-group v-model="form.checkoutIsCert">
              <el-radio v-for="dict in checkOutStatusOptions"  :label="dict.code" :key="dict.code">
                {{dict.name}}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="准考证" prop="admissionTicket">
            <el-radio-group v-model="form.admissionTicket">
              <el-radio v-for="dict in admissionTicketOptions"  :label="dict.code" :key="dict.code">
                {{dict.name}}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="理论合格分数" prop="theoryQualified">
            <el-input-number v-model="form.theoryQualified" controls-position="right" :min="0" :max="150" placeholder="请输入理论合格分数"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="实操合格分数"  prop="practicalQualified">
            <el-input-number v-model="form.practicalQualified" controls-position="right"  :min="0" :max="150" placeholder="请输入实操合格分数"/>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="理论总分数"  prop="theoryTotal">
            <el-input-number v-model="form.theoryTotal" controls-position="right" :min="0" :max="150" placeholder="请输入理论总分数"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="考试时长" prop="theoryDuration">
            <el-input-number v-model="form.theoryDuration" controls-position="right" :min="0" :max="180" placeholder="请输入考试时长(分钟)"/>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="补考间隔" prop="makeUpInterval">
            <el-input-number v-model="form.makeUpInterval" controls-position="right" :min="0" :max="90" placeholder="请输入补考间隔(天)"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="补考次数" prop="makeUpNumber">
            <el-input-number v-model="form.makeUpNumber" controls-position="right" :min="0" :max="5" placeholder="请输入补考次数"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="" prop="assessType">
            <el-checkbox  v-model="form.assessType" label="0" >参与矩阵评估</el-checkbox>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="projectClassify" label="军民机立项标识" style="margin-top: 5px" hidden>
            <el-input v-model="form.projectClassify=0"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="dialog-footer" slot="footer" style="text-align: center">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm">提交</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
    <el-dialog :title="title" :visible.sync="open" v-if="open" width="700px" append-to-body>
      <verify ref="verify" :process-status="processStatus" :historyList="historyList" @closeVerify="closeVerify"
              @submitVerify="submitVerify"></verify>
    </el-dialog>
  </div>
</template>

<script>
import { getProjectByProjectId, updateProject } from "@/api/question/projectInfo";
import verify from "@/views/workflow/components/Verify";
import {getAllNodeInfo} from "@/api/workflow/workFlow";

  //适用单位
  const applyUnitOptions = ['总装', '部装', '试飞', '检验检测'];
  //适用员工
  const applyStaffOptions = ['操作', '检验', '技术', '管理', '专业/专制', '非专业/兼职'];
  export default {
    name: "armyProject",
    props: {
      businessId: String,
      processParams: {},
      // 流程历史记录
      historyList: Array
    },
    components: {
      verify
    },
    data() {
      return {
        // 流程状态（0：启动流程，1：流程流转）
        processStatus: "1",
        // 审批页面开关
        open: false,
        // 业务标题
        title: '',
        // 流程实例
        processInstanceId: '',
        // 按钮loading
        buttonLoading: false,
        // 遮罩层
        loading: false,
        // 适用项目
        applyProjectOptions: [],
        //适用单位
        applyUnitOptions: applyUnitOptions,
        applyUnit: [],
        //适用员工
        applyStaffOptions: applyStaffOptions,
        applyStaff: [],
        // 培训大纲数据字典
        outlineStatusOptions: [],
        // 培训教材数据字典
        textbookStatusOptions: [],
        // 颁证机构数据字典
        institutionStatusOptions: [],
        // 民机取证类型数据字典
        civilStatusOptions: [],
        // 检验检测是否取证
        checkOutStatusOptions: [],
        // 准考证类型数据字典
        admissionTicketOptions: [],
        // 清册标识数据字典
        bookTagOptions: [],
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          projectName: [
            {required: true, message: "项目名称不能为空", trigger: "blur"}
          ],
          reviewPeriod: [
            {required: true, message: "复核期,单位:年,不能为空", trigger: "blur"}
          ],
          applyProject: [
            {required: true, message: "适用项目不能为空", trigger: "blur"}
          ],
          outline: [
            {required: true, message: "培训大纲不能为空", trigger: "change"}
          ],
          textbook: [
            {required: true, message: "培训教材不能为空", trigger: "blur"}
          ],
          institution: [
            {required: true, message: "颁证机构不能为空", trigger: "blur"}
          ],
          institutionName: [
            {required: true, message: "请输入机构名称", trigger: "blur"}
          ],
          checkoutIsCert: [
            { required: true, message: "检验检测员是否取证", trigger: "blur" }
          ],
          bookTag: [
            {required: true, message: "请选择清册标识！", trigger: "blur"}
          ],
          theoryQualified: [
            { required: true, message: "请输入理论合格分数", trigger: "blur" }
          ],
          theoryTotal: [
            { required: true, message: "请输入理论总分数", trigger: "blur" }
          ],
          theoryDuration: [
            { required: true, message: "请输入理论考试时长", trigger: "blur" }
          ],
        },
        taskVariables: {}, //流程变量
        //消息提醒
        sendMessage: {}
      };
    },
    watch: {
      businessKey: {
        immediate: true, // 很重要！！！
        handler(newVal) {
          if (newVal) {
            this.getById()  // 根据父页面传递参数
          }
        }
      }
    },
    created() {
      //查询培训大纲字典状态
      this.getDicts("question_project_outline").then(response => {
        this.outlineStatusOptions = response.responseBody.result;
      });
      //查询培训教材字典状态
      this.getDicts("question_project_textbook").then(response => {
        this.textbookStatusOptions = response.responseBody.result;
      });
      //查询颁证机构字典状态
      this.getDicts("question_project_institution").then(response => {
        this.institutionStatusOptions = response.responseBody.result;
      });
      //查询民机取证类型字典状态
      this.getDicts("question_project_civil").then(response => {
        this.civilStatusOptions = response.responseBody.result;
      });
      //查询适用项目字典
      this.getDicts("Q00001").then(response => {
        this.applyProjectOptions = response.responseBody.result;
      });
      //查询检验检测是否取证状态
      this.getDicts("question_project_checkout").then(response => {
        this.checkOutStatusOptions = response.responseBody.result;
      });
      //查询清册标识数据状态
      this.getDicts("exam_book_tag").then(response => {
        this.bookTagOptions = response.responseBody.result;
      });
      //查询准考证类型数据状态
      this.getDicts("question_project_admission").then(response => {
        this.admissionTicketOptions = response.responseBody.result;
      });
      this.getList()
    },
    methods: {
      // 取消按钮
      cancel() {
        this.$emit("closeForm")
      },
      callSubmit() {
        this.$emit("closeForm")
      },
      async getById() {
        // console.log("======"+this.businessKey);
        const {data} = await getProjectByProjectId(this.businessKey)
        this.form = data;
        this.applyUnit = this.form.applyUnit.split(',');
        this.applyStaff = this.form.applyStaff.split(',');
        this.form.assessType = this.form.assessType == "0" ? true : false;
      },
      getList() {
        this.loading = true
        // 除项目名称和复核期外其余属性均可读写
        this.disabled = false;
        //项目名称和复核期属性均可读写
        this.disabled2 = false;
        // 暂存按钮显示
        this.hideBtn = true;
        getProjectByProjectId(this.businessId).then(response => {
          this.form = response.responseBody;
          this.dataType();
        }).finally(() => {
          this.loading = false
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.assessType = this.form.assessType == true ? 0 : 1;
            this.form.applyUnit = this.applyUnit.toString();
            this.form.applyStaff = this.applyStaff.toString();
            this.buttonLoading = true;
            updateProject(this.form).then(response => {
              // this.msgSuccess("修改成功");
              this.submitProcess(response.responseBody)
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        });
      },

      //提交流程
      async submitProcess(entity) {
        const queryParams = {}
        queryParams.processId = this.processParams.processId
        queryParams.docUnid = this.processParams.docUnid
        queryParams.currentNodeId = this.processParams.currentNodeId
        queryParams.formData = JSON.stringify({ 'hasInspectors': entity.checkoutIsCert })
        // 获取所有节点信息
        await getAllNodeInfo(queryParams).then(resp => {
          const allNodes = resp.responseBody
          allNodes.forEach(item => {
            // 如果当前节点是‘发起流程’节点，在更新流程状态
            if (item.Nodeid === this.processParams.currentNodeId && item.NodeName === '发起流程') {
              // processStatus：0：发起流程，1：流程流转
              this.processStatus === '0'
            }
          })
        })
        // 路由条件
        this.processParams.formData = JSON.stringify({ 'hasInspectors': entity.checkoutIsCert });
        this.title = entity.projectClassify == 0 ? '军民机立项业务' : '行业线立项业务'
        this.open = true
        // 初始化提交审
        this.$nextTick(() => {
          this.$refs.verify.init(this.processParams);
        })
      },
      submitVerify() {
        this.open = false
        this.$emit("submitVerify");
      },
      closeVerify() {
        this.open = false
        this.$emit("closeForm")
      },

      // 数据类型转换
      dataType() {
        this.applyUnit = this.form.applyUnit !== null ? this.form.applyUnit.split(',') : this.applyUnit;
        this.applyStaff = this.form.applyStaff !== null ? this.form.applyStaff.split(',') : this.applyStaff;
        this.form.assessType = this.form.assessType == "0" ? true : false;
        this.$nextTick(() => {
          this.processInstanceId = this.form.processInstanceId
        })
      },
    }
  }
</script>
